package sz.szsmk.citizencard.model.network;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import sz.szsmk.citizencard.model.network.bean.request.CheckCodeRequest;
import sz.szsmk.citizencard.model.network.bean.request.CheckUserRequest;
import sz.szsmk.citizencard.model.network.bean.request.GetCodeRequest;
import sz.szsmk.citizencard.model.network.bean.request.LoginRequest;
import sz.szsmk.citizencard.model.network.bean.request.RegisterRequest;
import sz.szsmk.citizencard.model.network.bean.request.SetPsdRequest;
import sz.szsmk.citizencard.model.network.bean.response.CheckSmsResponse;
import sz.szsmk.citizencard.model.network.bean.response.ForgetResponse;
import sz.szsmk.citizencard.model.network.bean.response.GetCodeResponse;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.model.network.bean.response.UserBean;
import sz.szsmk.citizencard.model.network.bean.response.RegisterResponse;
import sz.szsmk.citizencard.model.network.dzbean.req.GetOpenIdReq;
import sz.szsmk.citizencard.model.network.dzbean.res.GetOpenIdRes;
import sz.szsmk.citizencard.util.Constant;

import java.io.IOException;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;

/**
 * 处理网络请求
 *
 * @author yang12345
 * @time 2017/3/17 下午4:29
 * @mail yangjunyan@xwtec.cn
 */

public class GsdxNetApi {
    private GsdxNetApiService gsdxNetApiService;
    public static GsdxNetApi instance;

    public static GsdxNetApi getInstance(OkHttpClient okHttpClient) {
        if (instance == null) {
            instance = new GsdxNetApi(okHttpClient);
        }
        return instance;
    }

    public GsdxNetApi(OkHttpClient okHttpClient) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 添加Rx适配器
                .addConverterFactory(GsonConverterFactory.create(buildGson())) // 添加Gson转换器
                .client(okHttpClient)
                .build();
        gsdxNetApiService = retrofit.create(GsdxNetApiService.class);
    }

    public Gson buildGson() {
        return new GsonBuilder().serializeNulls().registerTypeAdapterFactory(new
                NullStringToEmptyAdapterFactory()).create();
    }


    public class NullStringToEmptyAdapterFactory<T> implements TypeAdapterFactory {
        @SuppressWarnings("unchecked")
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            Class<T> rawType = (Class<T>) type.getRawType();
            if (rawType != String.class) {
                return null;
            }
            return (TypeAdapter<T>) new StringNullAdapter();
        }
    }

    public class StringNullAdapter extends TypeAdapter<String> {
        @Override
        public String read(JsonReader reader) throws IOException {
            if (reader.peek() == JsonToken.NULL) {
                reader.nextNull();
                return "";
            }
            return reader.nextString();
        }

        @Override
        public void write(JsonWriter writer, String value) throws IOException {
            if (value == null) {
                writer.nullValue();
                return;
            }
            writer.value(value);
        }
    }

    /**
     * 发送验证码
     *
     * @param loginRequest
     * @return
     */
    public rx.Observable<GetCodeResponse> getCode(@Body GetCodeRequest loginRequest) {
        return gsdxNetApiService.getCode(loginRequest);
    }

    public rx.Observable<IbaseBean> checkUser(@Body CheckUserRequest checkUserRequest) {
        return gsdxNetApiService.checkUser(checkUserRequest);
    }

    public rx.Observable<UserBean> loginByPassWord(@Body LoginRequest loginRequest) {
        return gsdxNetApiService.loginByPassWord(loginRequest);
    }


    /**
     * 注册
     *
     * @param setPsdRequest
     * @return
     */
    public rx.Observable<ForgetResponse> forgetPassWord(@Body SetPsdRequest setPsdRequest) {
        return gsdxNetApiService.forgetPassWord(setPsdRequest);
    }


    /**
     * 校验验证码
     *
     * @param checkCodeRequest
     * @return
     */
    public rx.Observable<CheckSmsResponse> checkSms(@Body CheckCodeRequest checkCodeRequest) {
        return gsdxNetApiService.checkSms(checkCodeRequest);
    }

    /**
     * @param registerRequest
     * @return
     */
    public rx.Observable<RegisterResponse> register(@Body RegisterRequest registerRequest) {
        return gsdxNetApiService.register(registerRequest);
    }

    public rx.Observable<GetOpenIdRes> getOpenId(@Body GetOpenIdReq getOpenIdReq) {
        return gsdxNetApiService.getOpenId(getOpenIdReq);
    }

}
